//228.汇总区间
//https://leetcode.cn/problems/summary-ranges/?envType=study-plan-v2&envId=top-interview-150
class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        int n = nums.size();
        if (n == 0) return {};
        vector<string> ret;
        int l = 0, r = 0;
        while (r < n)
        {
            if (r + 1 < n)
            {
                int a = nums[r + 1], b = nums[r];
                if (a < 0 || b < 0) //负数取余防止超出区间
                {
                    a %= 1000000;
                    b %= 1000000;
                }
                if (r + 1 < n && abs(a - b) > 1)
                {
                    if (l == r) ret.push_back(to_string(nums[r]));
                    else ret.push_back(to_string(nums[l]) + "->" + to_string(nums[r]));
                    l = r + 1;
                }
            }

            ++r;
        }
        if (l < r)
        {
            if (l == r - 1) ret.push_back(to_string(nums[r - 1]));
            else ret.push_back(to_string(nums[l]) + "->" + to_string(nums[r - 1]));
        }
        return ret;
    }
};